/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    unordered_map<int, int> list;
    int postorder(TreeNode* root)
    {
        if(!root)
            return 0;
        int sum = postorder(root->left) + postorder(root->right) + root->val;
        if(list.count(sum) > 0)
            list[sum] += 1;
        else
            list[sum] = 1;
        return sum;
    }
    vector<int> findFrequentTreeSum(TreeNode* root) {
        if(!root)
            return {};

        postorder(root);

        int max_count = 0;
        for(auto iter = list.begin(); iter != list.end(); iter++)
        {
            max_count = max(max_count, iter->second);
        }

        vector<int> res;
        for(auto iter = list.begin(); iter != list.end(); iter++)
        {
            if(iter->second == max_count)
                res.push_back(iter->first);
        }

        return res;
    }
};